package cn.zswltech.gruul.web.api.controller;

import cn.zswltech.gruul.api.RoleApi;
import cn.zswltech.gruul.biz.service.RoleService;
import cn.zswltech.gruul.common.dto.*;
import cn.zswltech.gruul.common.exception.BaseException;
import cn.zswltech.gruul.common.exception.RoleErrorEnum;
import cn.zswltech.gruul.common.exception.RoleException;
import cn.zswltech.gruul.common.result.MSG;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.common.entity.DisplayGroupDO;
import cn.zswltech.gruul.common.entity.RoleDO;
import cn.zswltech.gruul.web.api.controller.common.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@Slf4j
public class RoleController extends BaseController implements RoleApi {

    @Resource(name = "roleService")
    private RoleService roleService;



    /**
     * 创建角色
     *
     * @return
     */
    @Override
    @PostMapping
    public Response add(RoleAddREQ req) {
        try {
            roleService.createRole(req);
        } catch (RoleException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(RoleErrorEnum.ROLE_ADD_ERROR.getErrMsg(),e);
        }
        return Response.success();
    }

    /**
     * 查询应用下角色列表
     *
     * @return 角色列表
     */
    @Override
    @PostMapping("list")
    public Response<List<RoleDO>> getRolesByOrgCode() {
        List<RoleDO> roleDOS = null;
        try {
            roleDOS = roleService.getRolesBySystemCode();
        } catch (BaseException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(RoleErrorEnum.QUERY_ROLE_UNDER_SYSTEM_ERROR.getErrMsg(), e);
            return Response.error(RoleErrorEnum.QUERY_ROLE_UNDER_SYSTEM_ERROR);
        }
        return Response.success(roleDOS);
    }


    /**
     * 根据角色id删除指定角色
     *
     * @return
     */
    @Override
    @PostMapping("/delete")
    public Response deleteRole(RoleDelREQ req) {
        try {
            roleService.delete(req);
        } catch (BaseException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(RoleErrorEnum.ROLE_DEL_ERROR.getErrMsg(), e);
            return Response.error(RoleErrorEnum.ROLE_DEL_ERROR);
        }
        return Response.success();
    }

    /**
     * 修改角色基本信息
     *
     * @param req
     * @return
     */
    @Override
    @PutMapping
    public Response updateRole(RoleUpdateREQ req) {
        try {
            roleService.updateRuleById(req);
        } catch (BaseException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(RoleErrorEnum.ROLE_UPDATE_ERROR.getErrMsg(), e);
            return Response.error(RoleErrorEnum.ROLE_UPDATE_ERROR);
        }
        return Response.success();
    }

    /**
     * 查看角色详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("detail")
    public Response<RoleDO> detail(@Param("id") Long id) {
        if (id == null) {
            return Response.error(MSG.req_error_param);
        }
        RoleDO dRule = new RoleDO();
        dRule.setId(id);
        return Response.success(roleService.selectOne(dRule));
    }

    /**
     * 查看角色绑定菜单资源
     * @param req 角色id
     * @return
     */
    @Override
    public Response<RoleMenuResourceRSP> queryRoleResource(RoleMenuResourceREQ req) {
        RoleMenuResourceRSP rsp = null;
        try {
            rsp = roleService.queryRoleResource(req);
        } catch (BaseException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(RoleErrorEnum.QUERY_ROLE_MENU_RESOURCE_ERROR.getErrMsg(), e);
            return Response.error(RoleErrorEnum.QUERY_ROLE_MENU_RESOURCE_ERROR);
        }
        return Response.success(rsp);
    }


    /**
     * 角色对应用户列表
     * @param req 角色集合
     * @return
     */
    @Override
    public Response<List<Role2UserInfo>> getRole2Uers(Role2UserREQ req) {
        try {
            List<Role2UserInfo> result = roleService.getRole2Uers(req);
            return Response.success(result);
        } catch (BaseException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(RoleErrorEnum.ROLE_TO_USER_ERROR.getErrMsg(), e);
            return Response.error(RoleErrorEnum.ROLE_TO_USER_ERROR);
        }
    }
}
